<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>微博详情首页</title>
    <!-- 引入axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <!-- 引入Vue -->
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>

</head>

<body>
<div>
    <h1>微博详情首页</h1>
    <h2>{{weibo.nickname}}说:{{weibo.content}}</h2>
    <p>{{weibo.created}}</p>
    <img :src="weibo.url" alt="" width="300">
    <hr>
    <h3>相关评论:</h3>
    <div v-for="c in arr">
        {{c.nickname}}评论说:{{c.content}}
        <hr>
    </div>
    <input type="text" v-model="comment.content" placeholder="说点什么....">
    <input type="button" value="评论" @click="send()">
</div>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            weibo: {},
            comment: {
                content: "",
                weiboId: ""
            },
            arr: []
        },
        methods: {
            send() {
                // 给评论对象的里面的微博id赋值
                v.comment.weiboId = v.weibo.id;
                // 发请求把评论信息提交给服务器
                axios.post("/comment/insert", v.comment).then(function (response) {
                    if (response.data == 1) { // 登录的
                        alert("评论成功!")
                        // 刷新页面把新的评论显示出来
                        location.reload();
                    } else { // 未登录
                        alert("请先登录!");
                        location.href = "/login.html"
                    }
                })
            }
        },
        created: function () {
            // 获取请求微博详情 location.search代表?id=xxx
            axios.get("/weibo/selectById" + location.search).then(function (response) {
                v.weibo = response.data;
            })

            // 获取当微博相关的所有评论信息
            axios.get("/comment/selectByWeiboId"+location.search).then(function (response) {
                v.arr = response.data;
            })
        }
    })
</script>
</body>

</html>